package com.drojian.demo3.dataStorage.room;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import com.drojian.demo.R;

import java.util.List;

public class RoomActivity extends AppCompatActivity {
    WordDatabase wordDatabase;
    WordDao wordDao;
    TextView textView;
    Button buttonInsert,buttonDelete,buttonUpdata,buttonwClear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part3_activity_room);

        wordDatabase = Room.databaseBuilder(this, WordDatabase.class,"word_database")
                .allowMainThreadQueries().build(); // 强制让它在主线程运行
        wordDao = wordDatabase.getWordDao();
        textView = findViewById(R.id.textView);
        buttonInsert = findViewById(R.id.button_insert);
        buttonDelete= findViewById(R.id.button_delete);
        buttonUpdata = findViewById(R.id.button_updata);
        buttonwClear = findViewById(R.id.button_clear);
        // 插入
        buttonInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Word word1 = new Word("Hello", "你好");
                Word word2 = new Word("World", "世界");
                Word word3 = new Word("aa", "bb");
                Word word4 = new Word("aa", "bb");
                wordDao.insertWords(word1,word2,word3,word4);
                updataView();
            }
        });
        // 清空
        buttonwClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wordDao.deleteAllWords();
                updataView();
            }
        });
        // 修改
        buttonUpdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Word word = new Word("Thanks","谢谢");
                word.setChinese_meaning("bb");
                wordDao.updataWords(word);
                updataView();
            }
        });
        // 删除
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Word word = new Word("Hello", "你好");
                word.setChinese_meaning("你好");
                wordDao.deleteWords(word);
                updataView();
            }
        });

    }

    // 更新界面
    void updataView(){
        List<Word> list = wordDao.getAllWords();
        StringBuilder text = new StringBuilder();
        for(int i = 0 ; i < list.size() ; i++){
            Word word = list.get(i);
            text.append(word.getId()).append("、").append(word.getEnglish_word()).append(":").append(word.getChinese_meaning()).append('\n');
        }
        textView.setText(text);
    }

}